from django.shortcuts import render, redirect
from django import http
from django.shortcuts import render
from django.urls import reverse
from django.views import View
import json

# Create your views here.

# 重定向
class IndexView(View):
    '''
    测试重定向
    http://127.0.0.1:8000/index/
    '''
    def get(self,request):
        return http.HttpResponse('假装这是网站首页')


class LoginRedirectView(View):
    '''
    测试重定向
    http://127.0.0.1:8000/login_redirect/
    '''
    def get(self,request):

        # return redirect('/index/')
        # ret_url = reverse('总路由别名','子路由别名')
        ret_url = reverse('response:index')
        return redirect(ret_url)

class ResponseView(View):

    def get(self,request):

        # 子类JsonResponse
        person_data = {
            'name':'zhangsan',
            'age':29,
            'like':['打游戏','写代码'],
            'gender':0
        }
        person_data = [
            {
            'name': 'zhangsan',
            'age': 29,
            'like': ['打游戏', '写代码'],
            'gender': 0
            },
            {
                'name': 'zhangsan',
                'age': 29,
                'like': ['打游戏', '写代码'],
                'gender': 0
            }
        ]
        # 将dict转换成json_str
        # json_str = json.dumps(person_data)
        # return http.HttpResponse(content=json_str,content_type='application/json')

        return http.JsonResponse(person_data,safe=False)
        # 2. 属性
        # response = http.HttpResponse()
        # response.content = "响应对象的属性"
        # response.status_code = 303
        # return response

        # 1. 参数
        # content = "响应内容"
        # content_type = 'text/html;charset=utf-8'
        # status = 200
        # return http.HttpResponse(content,content_type,status)